{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "22bc2a2d-dd8d-4849-b174-a88b4147570f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试不同的 ChatOpenAI 导入方式:\n",
      "==================================================\n",
      "✅ 成功使用 langchain_openai.ChatOpenAI（推荐）\n",
      "✅ 成功使用 langchain_community.chat_models.ChatOpenAI\n",
      "⚠️  使用已弃用的 langchain.chat_models.ChatOpenAI\n",
      "\n",
      "==================================================\n",
      "总结:\n",
      "✅ 使用 langchain_openai.ChatOpenAI（官方推荐）\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\27727\\AppData\\Local\\Temp\\ipykernel_3136\\148971654.py:11: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-openai package and should be used instead. To use it run `pip install -U :class:`~langchain-openai` and import as `from :class:`~langchain_openai import ChatOpenAI``.\n",
      "  llm = ChatOpenAI(\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "ChatOpenAI 使用示例\n",
    "展示不同导入方式的区别\n",
    "\"\"\"\n",
    "\n",
    "def test_community_chat_openai():\n",
    "    \"\"\"使用 langchain_community 的方式\"\"\"\n",
    "    try:\n",
    "        from langchain_community.chat_models import ChatOpenAI\n",
    "        \n",
    "        llm = ChatOpenAI(\n",
    "            model=\"qwen-plus\",\n",
    "            openai_api_key=\"sk-4e88cf4db3e14894bafaff606d296610\",\n",
    "            openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "            temperature=0,\n",
    "            max_tokens=2000,\n",
    "            timeout=30\n",
    "        )\n",
    "        \n",
    "        print(\"✅ 成功使用 langchain_community.chat_models.ChatOpenAI\")\n",
    "        return llm\n",
    "        \n",
    "    except ImportError as e:\n",
    "        print(f\"❌ 导入失败: {e}\")\n",
    "        return None\n",
    "\n",
    "def test_new_chat_openai():\n",
    "    \"\"\"使用新的 langchain-openai 包的方式\"\"\"\n",
    "    try:\n",
    "        from langchain_openai import ChatOpenAI\n",
    "        \n",
    "        llm = ChatOpenAI(\n",
    "            model=\"qwen-plus\",\n",
    "            openai_api_key=\"sk-4e88cf4db3e14894bafaff606d296610\",\n",
    "            openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "            temperature=0,\n",
    "            max_tokens=2000\n",
    "        )\n",
    "        \n",
    "        print(\"✅ 成功使用 langchain_openai.ChatOpenAI（推荐）\")\n",
    "        return llm\n",
    "        \n",
    "    except ImportError as e:\n",
    "        print(f\"❌ 需要安装 langchain-openai: pip install langchain-openai\")\n",
    "        return None\n",
    "\n",
    "def test_deprecated_chat_openai():\n",
    "    \"\"\"使用已弃用的旧方式（不推荐）\"\"\"\n",
    "    try:\n",
    "        from langchain.chat_models import ChatOpenAI\n",
    "        \n",
    "        llm = ChatOpenAI(\n",
    "            model=\"qwen-plus\",\n",
    "            openai_api_key=\"sk-4e88cf4db3e14894bafaff606d296610\",\n",
    "            openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "            temperature=0\n",
    "        )\n",
    "        \n",
    "        print(\"⚠️  使用已弃用的 langchain.chat_models.ChatOpenAI\")\n",
    "        return llm\n",
    "        \n",
    "    except ImportError as e:\n",
    "        print(f\"❌ 旧方式可能已不可用: {e}\")\n",
    "        return None\n",
    "\n",
    "def main():\n",
    "    \"\"\"测试所有导入方式\"\"\"\n",
    "    print(\"测试不同的 ChatOpenAI 导入方式:\")\n",
    "    print(\"=\" * 50)\n",
    "    \n",
    "    # 测试新的推荐方式\n",
    "    llm1 = test_new_chat_openai()\n",
    "    \n",
    "    # 测试社区版方式\n",
    "    llm2 = test_community_chat_openai()\n",
    "    \n",
    "    # 测试已弃用的方式\n",
    "    llm3 = test_deprecated_chat_openai()\n",
    "    \n",
    "    print(\"\\n\" + \"=\" * 50)\n",
    "    print(\"总结:\")\n",
    "    if llm1:\n",
    "        print(\"✅ 使用 langchain_openai.ChatOpenAI（官方推荐）\")\n",
    "    elif llm2:\n",
    "        print(\"✅ 使用 langchain_community.chat_models.ChatOpenAI（兼容方案）\")\n",
    "    elif llm3:\n",
    "        print(\"⚠️  使用已弃用的 langchain.chat_models.ChatOpenAI（不建议）\")\n",
    "    else:\n",
    "        print(\"❌ 所有导入方式都失败\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11baadc6-4db7-4912-b6e5-4f7f63df6beb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rag_learn",
   "language": "python",
   "name": "rag_learn"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
